package TailRecusion;

// 4! = 4*3*2*1
public class Factorial {

    // 用递归实现阶乘

    public static int factorial(int n)
    {
        if (n == 1)
        {
            return 1;
        }
        else
        {
            return n * factorial(n - 1);
        }
    }


    // 用尾递归实现阶乘

    public static long TailFactorial(int n) {
        return factorialHelper(n, 1);
    }

    private static long factorialHelper(int n, long accumulator) {
        if (n <= 1) {
            return accumulator;
        } else {
            return factorialHelper(n - 1, n * accumulator);
        }
    }
}
